package cn.flying.cloud.flow.core.listener.event.callback;

import javax.annotation.Resource;

import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.impl.event.FlowableEntityExceptionEventImpl;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.job.service.impl.persistence.entity.JobEntityImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import cn.flying.cloud.flow.bo.FwProcExceptionBo;
import cn.flying.cloud.flow.service.FwProcExceptionService;

/**
 * 异常监听器
 *
 * @author: admin
 * @date: 2023年02月22日 23:33
 * @version: 1.0
 */
@Component
public class JobExecutionFailureListenerCallback implements EventListenerCallback {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private RuntimeService runtimeService;
    @Resource
    private FwProcExceptionService procExceptionService;

    /**
     * @param event
     */
    @Override
    public void execute(FlowableEvent event) {
        logger.info("==========执行异常开始监听器回调处理---start===========");
        System.out.println("事件类型：" + event.getType().name());
        FlowableEntityExceptionEventImpl entityEvent = (FlowableEntityExceptionEventImpl) event;
//        Throwable cause = entityEvent.getCause();
        JobEntityImpl entity = (JobEntityImpl) entityEvent.getEntity();
        ProcessInstance instance = runtimeService.createProcessInstanceQuery().processInstanceId(entityEvent.getProcessInstanceId()).singleResult();

        FwProcExceptionBo param = new FwProcExceptionBo();
        param.setProcInstId(entity.getProcessInstanceId());
        param.setExecutionId(entity.getExecutionId());
        FwProcExceptionBo exception = procExceptionService.getOneObj(param);
        if (exception == null) {
            exception = new FwProcExceptionBo();
            exception.setProcInstId(entity.getProcessInstanceId());
            exception.setProcInstName(instance.getName());
            exception.setExecutionId(entity.getExecutionId());
            String definitionId = entity.getProcessDefinitionId();
            exception.setProcDefId(definitionId);
            String[] split = definitionId.split(":");
            exception.setProcDefKey(split[0]);
            exception.setProcDefName(instance.getProcessDefinitionName());
            exception.setNodeId(entity.getElementId());
            exception.setNodeName(entity.getElementName());
            exception.setBizKey(instance.getBusinessKey());
            exception.setExceptionMsg(entity.getExceptionMessage());
            exception.setExceptionTrace(entity.getExceptionStacktrace());
            // N 未处理  Y 已处理
            exception.setDealState("N");
            exception.setTenantId(entity.getTenantId());

            procExceptionService.createObj(exception);
        } else {
            exception.setExceptionMsg(entity.getExceptionMessage());
            exception.setExceptionTrace(entity.getExceptionStacktrace());
            // N 未处理  Y 已处理
            exception.setDealState("N");
            procExceptionService.updateObj(exception);
        }
        logger.info("流程【{}】执行异常，实例ID：【{}】，流水号：【{}】", entityEvent.getProcessDefinitionId(), entityEvent.getProcessInstanceId(), instance.getBusinessKey());
        logger.info("==========执行异常开始监听器回调处理---end===========");
    }
}
